<?php
    /**
     * @param $filename 日志全路径
     * @param $data 日志内容
     * @param $mode 记录模式
     */
    function monitor_log($filename, $data, $mode)
    {
        if(is_array($data))
        {
            $data = var_export($data, true);
            file_put_contents($filename, $data ."\r\n", $mode);
        }

    }

    /**
     * 创建返回数据的格式
     * @code 状态码
     * @message 返回的消息说明
     * @data 返回的数据
     */
    function genBody($code, $message = '', $data = '')
    {
        $arr = array('code' => "$code");
        if( ! empty($message))
        {
            $arr['message'] = $message;
        }
        if( ! empty($data))
        {
            // 如果  $data 是数组，将数组中的所有的值转换为字符串类型兼容 IOS 端
            if(is_array($data))
            {
                strArrayVal($data);
            }else{
                $data = (string)$data;
            }
            $arr['data'] = $data;
        }
        return $arr;
    }


    /**
     * 将数组的所有值转换为字符串类型
     * @param $arr
     */
    function strArrayVal(&$arr){
        foreach ($arr as $k => $val) {
            if (is_array($arr[$k])) {
                strArrayVal($arr[$k]);
            } else {
                $arr[$k] = (string)$arr[$k];
            }
        }
    }


    /*
     * 判断开区时间
     * $arr_kaiqu  开区时间  1,2，
     * 1：一年内服 2：一到三年服 3：三年以上服
     * */

    function gettimelimit($data,$times){

        $time1 = strtotime("-1 year"); // 一年以前的时间戳
        $time2 = strtotime("-3 year");  // 三年以前的时间戳

        // 一年内服  >$time1  1-3:  >$time2 and <$time1  3:  <$time2
      //  $arr_time = [];
        if(is_array($times)){
            $arr_time = $times;
        }else{
            $arr_time[] = $times;
        }
//       $str_time = strtotime($time);
        if($arr_time){
            foreach($arr_time as $k=>$v){
                if($v){
                    $str_time = strtotime($v['etime']);
                    if($str_time > $time1){
                        $list_time[$k] = 1;
                    }else if($str_time < $time1 && $str_time > $time2){
                        $list_time[$k] = 2;
                    }else if($str_time < $time2){
                        $list_time[$k] = 3;
                    }
                }
            }

            $demand = '';
            foreach($data as $k=>$v){
                $demand[] = $v;
            }

            foreach($list_time as $k=>$v){
                if(in_array($v,$demand) != false){
                    $arr_kaiqu[$k] = $v;
                }
            }

            if($arr_kaiqu){

                foreach($arr_time as $k=>$v){
                    foreach($arr_kaiqu as $k1=>$v1){
                        if($k == $k1){
                            $arr_kaiqu[$k1] = $v;
                        }
                    }
                }

                if($arr_kaiqu){
                    return $arr_kaiqu;
                }else{
                    return false;
                }
            }else{

                return false;
            }

        }

    }


    /*
     * 链接数据库 返回要查找的数据
     * $dbname 要链接的数据库名 $tablename 要查询的数据表名
     * ***/

     function getsql($dbname,$tablename){

        $tbname = 'admin_'.$tablename;

        $con = mysqli_connect(C('DB_HOST'),C('DB_USER'),C('DB_PWD'),$dbname,C('DB_PORT'));

        $con->query("SET NAMES 'UTF8'");
        $sql = 'select * from '.$tbname;
        $res = mysqli_query($con,$sql);
        $num = mysqli_num_rows($res);

        if($num){
            for($i=0;$i<$num;$i++){
              $row = mysqli_fetch_array($res,MYSQLI_ASSOC);
              $table[$i] = $row;
            }
            return $table;
        }else{

            return false;
        }

     }

//判断是否开孔全开.$level等级$num开孔数
function  opening($level, $num)
{

    $rel = 0; //1为全开，0为没全开
    if (0 < $level and $level <= 30) {
        if ($num >= 1) {
            return $rel = 1;
        }
    } elseif ($level >= 40 and $level <= 60) {
        if ($num >= 2) {
            return $rel = 1;
        }
    } elseif ($level >= 70 and $level <= 90) {
        if ($num >= 3) {
            return $rel = 1;
        }
    } elseif ($level >= 100 and $level <= 120) {
        if ($num >= 4) {
            return $rel = 1;
        }
    } elseif ($level >= 130) {
        if ($num >= 5) {
            return $rel = 1;
        }
    } else {
        return $rel;
    }

}

//判断是否有伤害属性且顶尖差距不超过5%，$level 等级$hurt伤害
function hurt_differ($level, $hurt)
{
    $parameter = 0.95; //计算极品里的参数
    $best = 0; //返回状态，0为不符合，1符合
    switch ($level) {
        case 65:
            $ht = (36 * $parameter);
            if ($hurt >= $ht) {
                $best = 1;
                return $best;
            }
            break;
        case 75:
            $ht = (41 * $parameter);
            if ($hurt >= $ht) {
                $best = 1;
                return $best;
            }
            break;
        case 85:
            $ht = (46 * $parameter);
            if ($hurt >= $ht) {
                $best = 1;
                return $best;
            }
            break;
        case 95:
            $ht = (51 * $parameter);
            if ($hurt >= $ht) {
                $best = 1;
                return $best;
            }
            break;
        case 105:
            $ht = (56 * $parameter);
            if ($hurt >= $ht) {
                $best = 1;
                return $best;
            }
            break;
        case 115:
            $ht = (60 * $parameter);
            if ($hurt >= $ht) {
                $best = 1;
                return $best;
            }
            break;
        case 125:
            $ht = (65 * $parameter);
            if ($hurt >= $ht) {
                $best = 1;
                return $best;
            }
            break;
        case 135:
            $ht = (70 * $parameter);
            if ($hurt >= $ht) {
                $best = 1;
                return $best;
            }
            break;
        case 145:
            $ht = (75 * $parameter);
            if ($hurt >= $ht) {
                $best = 1;
                return $best;
            }
            break;
        default:
            return $best;
    }
    return $best;
}






    
    
    

    